/*
* Copyright 2018 Shanxi Lu'an Taihang Laihui Net Tech co.,LTD.
*/
package com.laihuichuxing.pool.impl;


import com.laihuichuxing.common.config.CacheConfig;
import com.laihuichuxing.mongo.client.GeoSpatialMongoClient;
import com.laihuichuxing.mongo.model.ConnModel;
import com.laihuichuxing.mq.constant.MongoConst;
import com.laihuichuxing.pool.UserLbs4MongoPool;
import com.laihuichuxing.pool.dto.DriverLbsDto;
import com.laihuichuxing.pool.dto.UserLbsDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.StampedLock;

/**
 * Description:
 *
 * @author: Qinxuefeng
 * @since: 1.0
 * Create Date Time: 2018/11/2 11:48
 * Update Date Time:
 */
@Service
public class UserLbs4MongoPoolImpl extends BaseLbs4Mongo implements UserLbs4MongoPool {

    @Autowired
    private MongoConst mongoConst;

    private final StampedLock sl = new StampedLock();
    private Logger logger = LoggerFactory.getLogger(UserLbs4MongoPoolImpl.class);

    @Override
    public GeoSpatialMongoClient getClient(String cityCode, String phone) {
        ConnModel connModel = new ConnModel(mongoConst.getHost(), mongoConst.getPort(), mongoConst.getUserName(), mongoConst.getPassword(), mongoConst.getDatabase());
        GeoSpatialMongoClient client = new GeoSpatialMongoClient(connModel);
        client.getCollection(CacheConfig.USERLBS + cityCode);
        return client;
    }

    @Override
    public UserLbsDto take(DriverLbsDto driverLbsDto) {
        List<Map<String,String>> lbsDtos = take(driverLbsDto, 1);
        if(lbsDtos.size()>0){
            UserLbsDto userLbsDto = new UserLbsDto();
            userLbsDto.setPhone(lbsDtos.get(0).get("mobile"));
            userLbsDto.setCityZone(driverLbsDto.getCityZone());
            return userLbsDto;
        }
        return null;
    }
    /*@Override
    public void del(String mobile,String cityCode) {
        super.del(mobile,RedisConfig.userLbs+cityCode);
    }*/
    @Override
    public List<Map<String,String>> take(DriverLbsDto driverLbsDto, int ListSize) {
        try {
            List<Map<String, String>> list = take(driverLbsDto.getCityZone(), driverLbsDto.getLongitude(),
                    driverLbsDto.getLatitude(),driverLbsDto.getPhone(), ListSize,
                    mongoConst.getMinDistance(), mongoConst.getMaxDistance());
            return list;
        } catch (Exception e) {
            logger.error("获取乘客出错,{}",e.toString(),e);
            return null;
        }
    }

    @Override
    public String takeMe(String mobile,String cityCode){
        try {
            return super.takeMe(mobile,cityCode);
        } catch (Exception e) {
            logger.error("获取自己出错,{}",e.toString(),e);
        }
        return "";
    }
    
}
